﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;
using System.Xml.Serialization;

namespace SharePointUsers
{
    public class HourGlass : IDisposable
    {
        public HourGlass()
        {
            Enabled = true;
        }
        public void Dispose()
        {
            Enabled = false;
        }
        public static bool Enabled
        {
            get { return Application.UseWaitCursor; }
            set
            {
                if (value == Application.UseWaitCursor) return;
                Application.UseWaitCursor = value;
                Form f = Form.ActiveForm;
                if (f != null && f.Handle != IntPtr.Zero)   // Send WM_SETCURSOR
                    SendMessage(f.Handle, 0x20, f.Handle, (IntPtr)1);
            }
        }
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    }
    public static class Helper
    {   
        #region Static Members
        public static string CSV_FILTER = "Comma Separated File |*.csv|All Files|*.*";
        public static string REPORT_DESIGN_FILTER = "Report Design |*.rdf|All Files|*.*";
        #endregion
        public static void CheckIfRunningAsAnAdmin(Form form)
        {
            //Check if the application is running in admin mode
            bool isElevated;
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);

            if (!isElevated)
            {
                DialogResult UserOutput = MessageBox.Show("SP User Manager is starting without administrator privilages.\n" +
                                    "This might prevent this application from connecting to your SharePoint farm.\n\n" +
                                    "Do you want to restart sp user manager with admin privilages?",
                                    "SP User Manager - Loading",
                                      MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (UserOutput == System.Windows.Forms.DialogResult.Yes)
                {
                    ProcessStartInfo StartInfo = new ProcessStartInfo(Application.ExecutablePath);
                    StartInfo.Verb = "runas";
                    try
                    {
                        Process.Start(StartInfo);
                    }
                    catch
                    {

                    }
                    finally
                    {
                        //ForceClose = true;
                        Application.Exit();
                    }
                }
                else if (UserOutput == System.Windows.Forms.DialogResult.Cancel)
                {
                    //ForceClose = true;
                    Application.Exit();
                }


            }
            else
                form.Text += " (Administrator)";

        }
    }
    [XmlRoot("dictionary")]
    public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
    {
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();

            if (wasEmpty)
                return;

            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                reader.ReadStartElement("item");

                reader.ReadStartElement("key");
                TKey key = (TKey)keySerializer.Deserialize(reader);
                reader.ReadEndElement();

                reader.ReadStartElement("value");
                TValue value = (TValue)valueSerializer.Deserialize(reader);
                reader.ReadEndElement();

                this.Add(key, value);

                reader.ReadEndElement();
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

            foreach (TKey key in this.Keys)
            {
                writer.WriteStartElement("item");

                writer.WriteStartElement("key");
                keySerializer.Serialize(writer, key);
                writer.WriteEndElement();

                writer.WriteStartElement("value");
                TValue value = this[key];
                valueSerializer.Serialize(writer, value);
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
        }
    }

}
